﻿using System.Collections.Generic;
using System.Data.Common;
using System.Threading.Tasks;

namespace Sidvall.Data
{
    public static class DataManager
    {
        #region Public Members

        #region ExecuteNonQueryAsync

        public static async Task<int> ExecuteNonQueryAsync(DbConnection connection, SqlExceptionBehavior exceptionBehavior, string commandText)
        {
            if (string.IsNullOrWhiteSpace(commandText))
                return 0;
            return await ExecuteNonQueryAsync(connection, exceptionBehavior, new string[] { commandText }).ConfigureAwait(false);
        }
        public static async Task<int> ExecuteNonQueryAsync(DbConnection connection, SqlExceptionBehavior exceptionBehavior, IEnumerable<string> commandTexts)
        {
            int value, tmp;

            if (commandTexts == null)
                return 0;
            value = 0;
            using (var command = connection.CreateCommand())
            {
                foreach (var commandText in commandTexts)
                {
                    if (!string.IsNullOrWhiteSpace(commandText))
                    {
                        command.CommandText = commandText;
                        switch (exceptionBehavior)
                        {
                            case SqlExceptionBehavior.Ignore:
                                try
                                {
                                    tmp = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                                }
                                catch
                                {
                                    tmp = 0;
                                }
                                break;
                            default:
                                tmp = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                                break;
                        }
                        value += tmp;
                    }
                }
            }
            return value;
        }

        #endregion
        #region ExecuteReaderAsync

        public static async Task<DbDataReader> ExecuteReaderAsync(DbConnection connection, string commandText)
        {
            if (string.IsNullOrWhiteSpace(commandText))
                throw new System.ArgumentException(nameof(commandText));
            var command = connection.CreateCommand();
            command.CommandText = commandText;
            return await command.ExecuteReaderAsync().ConfigureAwait(false);
        }

        #endregion
        #region ExecuteScalarAsync

        public static async Task<object> ExecuteScalarAsync(DbConnection connection, SqlExceptionBehavior exceptionBehavior, string commandText)
        {
            if (string.IsNullOrWhiteSpace(commandText))
                return null;
            var values = await ExecuteScalarAsync(connection, exceptionBehavior, new string[] { commandText }).ConfigureAwait(false);
            if (values.Length == 0)
                return 0;
            return values[0];
        }
        public static async Task<object[]> ExecuteScalarAsync(DbConnection connection, SqlExceptionBehavior exceptionBehavior, IEnumerable<string> commandTexts)
        {
            object value;

            var values = new List<object>();
            if (commandTexts == null)
                return values.ToArray();
            using (var command = connection.CreateCommand())
            {
                foreach (var commandText in commandTexts)
                {
                    if (!string.IsNullOrWhiteSpace(commandText))
                    {
                        command.CommandText = commandText;
                        switch (exceptionBehavior)
                        {
                            case SqlExceptionBehavior.Ignore:
                                try
                                {
                                    value = await command.ExecuteScalarAsync().ConfigureAwait(false);
                                }
                                catch
                                {
                                    value = null;
                                }
                                break;
                            default:
                                value = await command.ExecuteScalarAsync().ConfigureAwait(false);
                                break;
                        }
                        values.Add(value);
                    }
                }
            }
            return values.ToArray();
        }

        #endregion

        #endregion
    }
}
